package org.goldfish.demo.apicall.analysis.code;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import lombok.extern.slf4j.Slf4j;
import org.goldfish.demo.apicall.analysis.code.di.SpringFXMLLoader;
import org.goldfish.demo.apicall.analysis.code.select.ProjectSelectorController;
import org.goldfish.demo.apicall.analysis.code.upload.AnalysisController;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class CodeAnalyzeController {

    @FXML
    private ScrollPane projectSelectPane;

    @FXML
    private ScrollPane analysisResultPane;

    private ProjectSelectorController projectSelectorController;

    private AnalysisController analysisController;


    @FXML
    private void initialize() {
        try {
            SpringFXMLLoader projectLoader = new SpringFXMLLoader("project_selector_pane.fxml");
            Parent projectSelect = projectLoader.load();
            projectSelectorController = projectLoader.getController();
            projectSelectPane.setContent(projectSelect);

            SpringFXMLLoader analysisLoader = new SpringFXMLLoader("analysis_result.fxml");
            Parent analysisResult = analysisLoader.load();
            analysisController = analysisLoader.getController();
            analysisResultPane.setContent(analysisResult);
            
            // 将AnalysisController注入到ProjectSelectorController中
            projectSelectorController.setAnalysisController(analysisController);
        } catch (IOException e) {
            log.error("加载子面板失败", e);
        } catch (ClassCastException e) {
            log.error("加载子面板失败", e);
        }
    }

    @FXML
    private void onSearchButtonClick() {
        // 业务逻辑代码
    }

    @FXML
    private void onAnalysisButtonClick() {
        // 修改为使用路径列表
        try {
            analysisController.execute(projectSelectorController.getSelectedPaths());
        } catch (IOException e) {
            log.error("分析失败", e);
        }
        // 业务逻辑代码
    }
}